class User(object):
    def __init__(self, name, age):
        self._name = name  # _:受保护的变量
        self.__age = age  # __:私有的变量

    @property  # 获取变量，函数变成变量了
    def age(self):
        return self.__age

    @age.setter  # 变量的修改器
    def age(self, age):
        if isinstance(age, int):
            self.__age = age
        else:
            raise Exception("年龄请输入整数")

    def __show_infos(self):
        print('大家好，我叫%s,今年%d岁' % (self._name, self.__age))


lily = User('lily', 22)
# print(lily._name)
# print(lily.__age)  # error
# lily.__show_infos()  # error
# print(lily.age)  # 22
# lily.age = 24
# print(lily.age)  # 25


# lily.set_age("25")  # error


class Player(object):
    numbers = 0  # 类属性 （不属于单个实例）
    levels = ['青铜', '白银', '黄金', '钻石', '王者']

    def __init__(self, name, age, city, level):  # 初始化函数（构造函数
        self.__name = name
        self.__age = age
        self.__city = city
        self.level = level
        Player.numbers += 1  # 统计玩家人数
        if level not in Player.levels:
            raise Exception("段位设置错误！")

    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self, name):
        if name == self.name:  # property
            raise Exception("修改名字与当前名字相同，请重新修改")
        else:
            self.__name = name

    @property
    def age(self):
        return self.__age

    @age.setter
    def age(self, age):
        if not isinstance(age, int):
            raise Exception("年龄必须整数")
        elif age < 0 or age > 100:
            raise Exception('年龄必须在0到100间')
        else:
            self.__age = age

    def level_up(self):
        i = Player.levels.index(self.level)
        if i < len(Player.levels) - 1:
            self.level = Player.levels[i + 1]
            print("已升级，当前等级为", self.level)
        else:
            print("您的等级已是最高级,无法升级")

    def show(self):  # 实例的方法
        print('我是王者游戏的第%d个玩家，我的名字是%s,我来自%s' % (Player.numbers, self.name, self.city))

    def get_weapon(self, weapon):
        self.weapon = weapon

    def show_weapon(self):
        return self.weapon.show_weapon()

    # 类方法
    @classmethod
    def get_players(cls):  # 类方法
        print("玩家数量到达了%d人" % cls.numbers)

    @staticmethod
    def isValid(**kwargs):
        if kwargs['age'] > 18:
            return True
        else:
            return False

try:
    jack = Player('jack',18,'shanghai','钻石')

    print(jack.name)
    print(jack.age)
    jack.age = 11
    print(jack.__dict__)
except Exception as e:
    print(e)
